/**
 * 用户数据action
 */

define([
    'vue',
    'vuex/mutation-types',
    'api/user'
], function(Vue, types, userApi){

    return {
        setLoginUser: function(store, user) {
            if(user) {
                store.dispatch(types.RECEIVE_LOGIN_USER, user)
            }else{
                userApi.getMe(function(user){
                    store.dispatch(types.RECEIVE_LOGIN_USER, user)
                })
            }
        },
        getUserModuleTree: function(store, cb){
            userApi.getUserModuleTree(function(modules){
                store.dispatch(types.RECEIVE_USER_MODULE_TREE, modules)
                cb()
            }, function(error){
            })
        },
        updateTreeByPath: function(store, path){
            store.dispatch(types.UPDATE_TREE_BY_PATH, path)
        },
        updateTreeById: function(store, id, active){
            store.dispatch(types.UPDATE_TREE_BY_ID, id, active)
        },
        updateSearchUserKeywords: function(store, keywords){
            store.dispatch(types.UPDATE_SEARCH_USER_KEYWORDS, keywords)
        },
        resetCurrentUser: function(store) {
            store.dispatch(types.RESET_CURRENT_USER)
        },
        setCurrentUser: function(store, id, cb) {
            // 查看已有用户列表中是否存在该ID所指用户
            var user = null
            var userList = store.state.user.page.list
            for( var i=0; i < userList.length; i++ ) {
                if(userList[i].id == id) {
                    user = userList[i]
                    break
                }
            }
            if(user == null) {
                userApi.getUser(id, function(user){
                    store.dispatch(types.SET_CURRENT_USER, user, -1)
                    if(cb) cb(user)
                }, function(error){
                    alert(error)
                    router.go('/user')
                })
            } else {
                store.dispatch(types.SET_CURRENT_USER, user, i)
                if(cb) cb(user)
            }
        },
        resetResult: function(store) {
            store.dispatch(types.ADD_USER_REQUEST)
        },
        /**
         * 获取用户数据
         *  1. 强制重新获取：lastTimestamp === -1 或者 force === true时
         *      获取最新数据的第一页结果 （不向后台传递参数：lastTs）
         *  2. 追加数据：lastTimestamp !== -1 且 force !== true, 且 next === true 时
         *      获取下一页数据，向后台传递参数：lastTs，pageNumber，后台返回 lastTs 之前的分页数据 和 lastTs 之后的更新数据
         *  3. 获取更新：lastTimestamp !== -1 且 force !== true, 且 next !== true 时
         *      获取更新数据，向后台传递参数：lastTs，后台返回 lastTs 之后的更新数据
         *
         * @param store
         * @param force
         * @param next
         */
        getAllUsers: function(store, force, next) {
            var user = store.state.user
            var params = {
                keywords: user.page.keywords
            }
            if(user.page.lastTimestamp === -1 || force) {
                // 强制重新获取
            } else {
                if(Object.keys(user.updated).length > 0) {
                    params['updated'] = JSON.stringify(user.updated)
                }
                params['lastTs'] = user.page.lastTimestamp
                if(next) {
                    params['pn'] = user.page.pageNumber + 1
                }
            }
            userApi.getUsers(params,function(data, ts){
                store.dispatch(types.RECEIVE_USERS, data, ts)
            })
        },
        addUser: function(store, user) {
            store.dispatch(types.ADD_USER_REQUEST)
            userApi.saveUser(user, function(user){
                store.dispatch(types.ADD_USER_SUCCESS, user)
            }, function(error){
                store.dispatch(types.ADD_USER_FAILURE, error)
            })
        },
        editUser: function(store, id, changed, cb) {
            store.dispatch(types.EDIT_USER_REQUEST)
            userApi.updateUser(id, changed, function(user){
                if(cb) cb(user)
                store.dispatch(types.EDIT_USER_SUCCESS, user)
            }, function(error){
                store.dispatch(types.EDIT_USER_FAILURE, error)
            })
        }
    }
})